package com.dianxin.smartpark.service.park.impl;

import java.util.Date;
import java.util.List;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.park.SmartParkCarPark;
import com.dianxin.smartpark.dao.dataobject.park.SmartParkCarParkExample;
import com.dianxin.smartpark.dao.mapper.park.SmartParkCarParkMapper;
import com.dianxin.smartpark.dao.qo.park.SmartParkCarParkQO;
import com.dianxin.smartpark.service.park.SmartParkCarParkService;
import com.dianxin.smartpark.service.vo.park.SmartParkCarParkVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 停车场业务层处理
 * 
 * @author chenjiasong
 * @date 2021-07-28
 */
@Service
public class SmartParkCarParkServiceImpl implements SmartParkCarParkService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkCarParkServiceImpl.class);
    @Resource
    private SmartParkCarParkMapper smartParkCarParkMapper;

    /**
     * 查询停车场
     * 
     * @param qo 停车场ID
     * @return 停车场
     */
    @Override
    public CommonResponseBody<SmartParkCarParkVO> selectSmartParkCarPark(SmartParkCarParkQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkCarParkExample smartParkCarParkExample = new SmartParkCarParkExample();
        SmartParkCarParkExample.Criteria criteria = smartParkCarParkExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getParkName() != null) {
            criteria.andParkNameLike(qo.getParkName()+ "%");
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkCarPark> passRecords = smartParkCarParkMapper.selectByExample(smartParkCarParkExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkCarParkVO.class);
    }

    /**
     * 查询停车场列表
     * 
     * @param qo 停车场
     * @return 停车场
     */
    @Override
    public CommonResponseBody<List<SmartParkCarParkVO>> selectSmartParkCarParkList(SmartParkCarParkQO qo)
    {
        SmartParkCarParkExample smartParkCarParkExample = new SmartParkCarParkExample();
        SmartParkCarParkExample.Criteria criteria = smartParkCarParkExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkCarParkExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkCarParkExample.setOrderByClause("gmt_create desc");
        }
        List<SmartParkCarPark> passRecords = smartParkCarParkMapper.selectByExample(smartParkCarParkExample);
        long count = smartParkCarParkMapper.countByExample(smartParkCarParkExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkCarParkVO.class);
    }

    /**
     * 新增停车场
     * 
     * @param vo 停车场
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkCarPark(SmartParkCarParkVO vo)
    {
        // 验证 todo
        SmartParkCarPark smartParkCarPark = new SmartParkCarPark();
        try {
            BeanUtil.insertConversion(vo, smartParkCarPark);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkCarPark.beforeInsert();
        return ResponseBodyWrapperUtil.toResData(smartParkCarParkMapper.insertSelective(smartParkCarPark));
    }

    /**
     * 修改停车场
     * 
     * @param vo 停车场
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkCarPark(SmartParkCarParkVO vo)
    {
        // 验证 todo
        SmartParkCarPark smartParkCarPark = new SmartParkCarPark();
        try {
            BeanUtil.insertConversion(vo, smartParkCarPark);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkCarPark.beforeUpdate();
        return ResponseBodyWrapperUtil.toResData(smartParkCarParkMapper.updateByPrimaryKeySelective(smartParkCarPark));
    }

    /**
     * 删除停车场信息
     * 
     * @param vo 停车场
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkCarPark(SmartParkCarParkVO vo)
    {
        // 验证 todo
        if(StringUtils.isNull(vo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkCarPark smartParkCarPark = new SmartParkCarPark();
        try {
            BeanUtil.insertConversion(vo, smartParkCarPark);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkCarPark.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkCarParkMapper.updateByPrimaryKeySelective(smartParkCarPark));
    }
}
